package com.mango.game.dtos;

import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.http.ResponseEntity;

@Data
@Builder
@NoArgsConstructor
public class ResponseDTO {

    // 业务代码 0-正常 其他异常
    private Integer code;

    // 业务消息
    private String message;

    // 响应数据
    private Object data;

    // 分页信息（列表时存在）
    private ResponsePageDTO page;

    public ResponseDTO(Integer code, String message) {
        this.code = code;
        this.message = message;
        this.data = null;
        this.page = null;
    }

    public ResponseDTO(Integer code, String message, Object data, ResponsePageDTO page) {
        this.code = code;
        this.message = message;
        this.data = data;
        this.page = page;
    }

    public ResponseDTO(IPage<?> iPage) {
        this.code = 0;
        this.message = "success";
        this.data = iPage.getRecords();

        this.page = new ResponsePageDTO((int) iPage.getTotal(), (int) iPage.getCurrent(), (int) iPage.getSize(), (int) iPage.getPages());
    }

    public static ResponseEntity<ResponseDTO> success(String message) {
        return success(message, null);
    }

    public static ResponseEntity<ResponseDTO> success(String message, Object data) {
        return ResponseEntity.ok(new ResponseDTO(0, message, data, null));
    }

    public static ResponseEntity<ResponseDTO> failure(String message) {
        return ResponseEntity.ok(new ResponseDTO(-1, message, null, null));
    }

}
